import { createRouter, createWebHashHistory } from "vue-router";
// 进度条加载
import nprogress from "nprogress";
import "nprogress/nprogress.css";
import { toast } from "../tips/index";
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      // redirect:"/home",
      name: "home",
      meta: {
        title: "主页",
      },
      component: () => import("../views/Home.vue"),
    },
    {
      path: "/login",
      name: "login",
      meta: {
        title: "登录",
      },
      component: () => import("../views/Login.vue"),
    },
    {
      meta: {
        title: "404",
      },
      path: "/:pathMatch(.*)*",
      component: () => import("../views/404.vue"),
    },
    // 注册
    {
      path: "/Register",
      name: "/Register",
      meta: {
        title: "注册",
      },
      component: () => import("../views/Register.vue"),
    },
  ],
});
// 异步路由
const asyncRoutes = [
  {
    path: "/",
    name: "/",
    meta: {
      title: "首页",
    },
    component: () => import("../views/asyncElement/HomeSon.vue"),
  },
  {
    path: "/recycleOrder/admin",
    name: "/recycleOrder/admin",
    meta: {
      father: "订单管理",
      title: "回收订单管理",
    },
    component: () => import("../views/asyncElement/RecycleOrder.vue"),
  },
  {
    path: "/conversionOrder/admin",
    name: "/conversionOrder/admin",
    meta: {
      father: "订单管理",
      title: "兑换订单管理",
    },
    component: () => import("../views/asyncElement/ConversionOrder.vue"),
  },
  {
    path: "/equipment/apply",
    name: "/equipment/apply",
    meta: {
      father: "设备管理",
      title: "设备申请",
    },
    component: () => import("../views/asyncElement/EquipmentApply.vue"),
  },
  {
    path: "/equipment/EqapplyFor",
    name: "/equipment/EqapplyFor",
    meta: {
      father: "设备管理",
      fatherSon: "设备申请",
      title: "设备申请填写",
    },
    component: () => import("../views/asyncElement/EqapplyFor.vue"),
  },
  {
    path: "/equipment/admin",
    name: "/equipment/admin",
    meta: {
      father: "设备管理",

      title: "设备管理",
    },
    component: () => import("../views/asyncElement/EquipmentAdmin.vue"),
  },
  {
    path: "/equipment/monitoring",
    name: "/equipment/monitoring",
    meta: {
      father: "设备管理",
      title: "设备监控",
    },
    component: () => import("../views/asyncElement/Monitoring.vue"),
  },
  {
    path: "/inventory/admin",
    name: "/inventory/admin",
    meta: {
      father: "库存管理",
      title: "库存管理",
    },
    component: () => import("../views/asyncElement/Inventory.vue"),
  },
  {
    path: "/wallet/admin",
    name: "/wallet/admin",
    meta: {
      father: "财务管理",

      title: "钱包管理",
    },
    component: () => import("../views/asyncElement/Wallet.vue"),
  },
  {
    path: "/Recharge/admin",
    name: "/Recharge/admin",
    meta: {
      father: "财务管理",
      fatherSon: "钱包管理",

      title: "充值",
    },
    component: () => import("../views/asyncElement/Recharge.vue"),
  },
  {
    path: "/finance/charge",
    name: "/finance/charge",
    meta: {
      father: "财务管理",

      title: "财务核销",
    },
    component: () => import("../views/asyncElement/Finance.vue"),
  },
  {
    path: "/shop/admin",
    name: "/shop/admin",
    meta: {
      father: "系统管理",

      title: "门店管理",
    },
    component: () => import("../views/asyncElement/ShopAdmin.vue"),
  },
  {
    path: "/member/admin",
    name: "/member/admin",
    meta: {
      father: "系统管理",

      title: "成员管理",
    },
    component: () => import("../views/asyncElement/Member.vue"),
  },
  {
    path: "/orders/admin",
    name: "/orders/admin",
    meta: {
      father: "系统管理",
      title: "接单管理",
    },
    component: () => import("../views/asyncElement/OrdersAdmin.vue"),
  },
];

const data = [
  {
    path: "/Recharge/admin",
    frontPath: "/Recharge/admin",
  },
  {
    path: "/recycleOrder/admin",
    frontPath: "/recycleOrder/admin",
  },
  {
    path: "/conversionOrder/admin",
    frontPath: "/conversionOrder/admin",
  },
  {
    path: "/equipment/admin",
    frontPath: "/equipment/admin",
  },
  {
    path: "/equipment/apply",
    frontPath: "/equipment/apply",
  },
  {
    path: "/equipment/EqapplyFor",
    frontPath: "/equipment/EqapplyFor",
  },
  {
    path: "/equipment/monitoring",
    frontPath: "/equipment/monitoring",
  },
  {
    path: "/inventory/admin",
    frontPath: "/inventory/admin",
  },
  {
    path: "/wallet/admin",
    frontPath: "/wallet/admin",
  },
  {
    path: "/",
    frontPath: "/",
  },
  {
    path: "/finance/charge",
    frontPath: "/finance/charge",
  },
  {
    path: "/shop/admin",
    frontPath: "/shop/admin",
  },
  {
    path: "/member/admin",
    frontPath: "/member/admin",
  },
  {
    path: "/orders/admin",
    frontPath: "/orders/admin",
  },
];

// 添加路由
// 这里应该传入接口数据进行判断
// menus是接口数据
function generatorAsyncRoutes(menus) {
  let hasNewsR = false;
  function mapRoute(menus) {
    // console.log(menus);
    menus.forEach((item) => {
      // 在异步路由中找和接口数据中一样path的数据返回给hasR
      const hasR = asyncRoutes.find((asyncRoute) => {
        return asyncRoute.path === item.frontPath;
      });
      // 这里判断hasR不为空且基础路由表中没有改路径则新增路由
      if (hasR && !router.hasRoute(item.frontPath)) {
        // 在主页下新增路由
        router.addRoute("home", hasR);
        // 已经新增路由，给开关变量赋值true
        hasNewsR = true;
      }
      // 遍历子路由数据看有没有要新增的，有则再次传参调用函数
      if (item.child && item.child.length > 0) {
        mapRoute(item.child);
      }
    });
  }
  mapRoute(menus);
  return hasNewsR;
}
router.beforeEach((to, from, next) => {
  // 判断是否登录
  const token = localStorage.getItem("token");
  if (!token && to.path !== "/login" && to.path !== "/Register") {
    toast("请您先登录", "error");
    return next("/login");
  }
  // 避免重复登录
  if (token && (to.path == "/login" || to.path == "/Register")) {
    if (to.path == "/login") {
      toast("请勿重复登录", "error");
      return next({
        path: from.path ? from.path : "/",
      });
    }else if(to.path=="/Register"){
      toast("请您先退出登录","error")
      return next({
        path: from.path ? from.path : "/",
      })
    }
  }
    nprogress.start();
  if (to.meta) {
    document.title = to.meta.title;
  }
  let hasNewsR = false;
  // 传入模拟数据
  hasNewsR = generatorAsyncRoutes(data);
  hasNewsR ? next(to.fullPath) : next();
});
// router.afterEach((to,from)=>{
//   console.log(router);
// })
router.beforeEach((to, from) => {
  nprogress.done();
  if (to.meta) {
    document.title = to.meta.title;
  }
});

export default router;
